﻿using System;
using BBCode.Tags;
using BBCode.Template;

namespace BBCode.Nodes
{
    /// <summary>
    /// Smiley Nodes
    /// </summary>
    public class Smiley : INode
    {
        private readonly string _id = "[" + Guid.NewGuid() + "]";
        private readonly NodeIndex _nodeIndex;
        private readonly ITag _tag;
        private readonly string _text;

        #region properties

        /// <summary>
        /// Gets a value indicating whether this instance has parent.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance has parent; otherwise, <c>false</c>.
        /// </value>
        public bool HasParent
        {
            get { return Parent != null; }
        }

        /// <summary>
        /// Gets the ID.
        /// </summary>
        public string ID
        {
            get { return _id; }
        }

        /// <summary>
        /// Gets or sets the parent.
        /// </summary>
        /// <value>
        /// The parent.
        /// </value>
        public INode Parent { get; set; }

        /// <summary>
        /// Prints the specified smiley. Only implemented to facilitate the INode interface, should be changed in a refactoring session.
        /// </summary>
        /// <param name="shouldAutoParseLinks"></param>
        /// <returns></returns>
        public string Print(bool shouldAutoParseLinks)
        {
            return Print();
        }

        /// <summary>
        /// Gets the tag.
        /// </summary>
        public ITag Tag
        {
            get { return _tag; }
        }

        /// <summary>
        /// Gets the index of the node.
        /// </summary>
        /// <value>
        /// The index of the node.
        /// </value>
        public NodeIndex NodeIndex
        {
            get { return _nodeIndex; }
        }

        /// <summary>
        /// Gets the text.
        /// </summary>
        public string Text
        {
            get { return _text; }
        }

        #endregion

        #region constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Smiley"/> class.
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <param name="text">The text.</param>
        /// <param name="nodeIndex">Index of the node.</param>
        public Smiley(ITag tag, string text, NodeIndex nodeIndex)
        {
            _text = text;
            _nodeIndex = nodeIndex;
            _tag = tag;
        }

        #endregion

        /// <summary>
        /// Prints this instance.
        /// </summary>
        /// <returns>the parsed smiley.</returns>
        public string Print()
        {
            var tag = (Tags.Smiley) Tag;

            //Get the template, fill it and process it!.
            switch (tag.ParseType)
            {
                case ParseType.Clean:
                    //Clean the node, leaving the innertext
                    return string.Empty;
                case ParseType.Remove:
                    //Remove it ALL!
                    return string.Empty;
                default:
                    //Default is always to print it as it should be printed!
                    var template = tag.GetTemplate();
                    template["NAME"] = tag.Name;
                    template["ALLIAS"] = _text;
                    template["INNERTEXT"] = tag.URL;

                    return template.Process();
            }
        }
    }
}